<?php
class TinTucController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	$tintucMapper = Library_Model_ChuDeMapper::getInstance();
    	$tintuc = $tintucMapper->tintucList();
    	$this->view->tintuc = $tintuc;
    	
    	Zend_Registry::get('logger')->info($tintuc);
    }

    public function themAction()
    {
    	$this->view->title = 'Thêm tin tức';
    	$formThem = new Admin_Form_TinTucForm();
    	$formThem->setMethod('post');
    	$formThem->setAction('/zfMHB/public/admin/tintuc/them');
    	
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	$formThem->getElement('tenbaiviet')->addValidator('Db_NoRecordExists',true, array(
    			'table' => 'tbl_tintuc',
    			'field' => 'TenBaiViet'
    	));
    	$this->view->form = $formThem;
    	
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			
    			$machude 		= $data['machude'];
    			$mand 			= $data['mand'];
    			$tenbaiviet		= $data['tenbaiviet'];
    			$noidung 			= $data['noidung'];
    			$luotxem			= $data['luotxem'];
				$kiemduyet		= $data['kiemduyet'];
				$taptin				= $data['taptin'];
			
    			$tintucMapper = Library_Model_TinTucMapper::getInstance();
    			$tintucObj 	= new Library_Model_TinTuc();
    			    			
    			$tintucObj->setMachude($machude);
    			$tintucObj->setMand($mand);
    			$tintucObj->setTenbaiviet($tenbaiviet);
    			$tintucObj->setNoidung($noidung);
				$tintucObj->setNgaydang(Zend_Date::now()->get('yyyy-MM-dd HH:mm:ss'));
				$tintucObj->setLuotxem($luotxem);
				$tintucObj->setKiemduyet($kiemduyet);
				$tintucObj->setTaptin($taptin);
				
    	
    			$tintuc = $tintucMapper->save($tintucObj);
    			$this->_redirect('/admin/tintuc/index');
    		}
    	}
    }

    public function xemAction(){
    	$tt_id = $this->_request->getParam('id');
    		
    	$tintucMapper = Library_Model_TinTucMapper::getInstance();
    	$tintuc = $tintucMapper->searchTinTucById($tt_id);
    	$this->view->tintuc = $tintuc;
    }
    }